//
//  AppDelegate.swift
//  ExtremeHealth
//
//  Created by Zhongkai Li 李忠凯 on 2018/8/21.
//  Copyright © 2018 GanDongKeJi. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    var wxLogin: WXLogin!

    var reachability: EHReachability!

    func application(_ application: UIApplication,
                     didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        setUpCoordinator()
        setUpWindow(rootVC: MainCoordinator.shared.navigationController)
        login()
        register()
        return true
    }

    func application(_ app: UIApplication, open url: URL,
                     options: [UIApplicationOpenURLOptionsKey: Any] = [:]) -> Bool {
        if wxLogin.canHandle(url.scheme) {
            return wxLogin.handleOpen(url)
        }
        return true
    }

    private func setUpCoordinator() {
        MainCoordinator.shared.start()
    }

    private func setUpWindow(rootVC: UIViewController) {
        window = UIWindow(frame: UIScreen.main.bounds)
        window?.rootViewController = rootVC
        window?.makeKeyAndVisible()
    }

    private func login() {
        AppUser.current.setUp()
    }

    private func register() {
        registerWX()
        registerReachability()
    }

    private func registerWX() {
        wxLogin = WXLogin()
    }

    private func registerReachability() {
        reachability = EHReachability()
        reachability.setUp()
    }
}
